<?php
/**
 * Related Products
 *
 * @author 		WooThemes
 * @package 	WooCommerce/Templates
 * @version     1.6.4
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

global $product, $woocommerce_loop;

$related = $product->get_related(5);

global $counts;

$counts = count($related);

if ( sizeof( $related ) == 0 ) return;

$args = apply_filters('woocommerce_related_products_args', array(
	'post_type'				=> 'product',
	'ignore_sticky_posts'	=> 1,
	'no_found_rows' 		=> 1,
	'posts_per_page' 		=> 5,
	'orderby' 				=> 'rand',
	'post__in' 				=> $related,
	'post__not_in'			=> array($product->id)
) );
$products = new WP_Query( $args );
//var_dump($products);
$i = 0;
$j = 0;
$k = 0;
if ( $products->have_posts() ) : ?>

	<div class="product_you_like">
		<div class="product-related-title">
			<?php _e( 'You might also like', 'woocommerce' ); ?>
		</div>
				<?php while ( $products->have_posts() ) : $products->the_post(); ?>
				<?php global $product, $post, $wpdb, $average; ?>
				<?php 
				$new_product = get_post_meta( $post->ID, 'new_product', true );
				$count = $wpdb->get_var("
				SELECT COUNT(meta_value) FROM $wpdb->commentmeta
				LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
				WHERE meta_key = 'rating'
				AND comment_post_ID = $post->ID
				AND comment_approved = '1'
				AND meta_value > 0
			");

			$rating = $wpdb->get_var("
				SELECT SUM(meta_value) FROM $wpdb->commentmeta
				LEFT JOIN $wpdb->comments ON $wpdb->commentmeta.comment_id = $wpdb->comments.comment_ID
				WHERE meta_key = 'rating'
				AND comment_post_ID = $post->ID
				AND comment_approved = '1'
			");
			?>
			
			<div class="item">
			
					<div class="feature-content">
						<div class="feature-img">
							<?php
								if( $new_product == 'yes' ){
									echo '<span class="new-product"></span>';
								}
							?>
							<?php if(has_post_thumbnail()) {
								the_post_thumbnail('shop_catalog'); 
							} ?>
							
						</div>
						<div class="feature-meta">
							<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
							<p><?php echo $product->get_price_html(); ?></p>
						</div>
					</div>
				</div>

				<?php 
					endwhile; // end of the loop. 
					wp_reset_query();
				?>
	</div>
	
<?php
endif;
wp_reset_postdata(); ?>
<div class="clearfix"></div>
